/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.view;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import offset.nodes.Constants;
import offset.nodes.server.config.NodesConfig;

/**
 *
 * @author Walter Lütgenau
 */
public class NodesBean {
    public static final String DEFAULT_HOMEPAGE_URL = "http://www.nodes-development-cms.net";
    
    /*
     * Shall n_substituteApplet be used? This increases the page size,
     * but saves a server roundtrip on applet invocation
     */
    boolean appletSubstitution = true;
    String clientConfigFiles;
    
    class AppletCallWrapper implements Map {

        @Override
        public int size() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public boolean isEmpty() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public boolean containsKey(Object key) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public boolean containsValue(Object value) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Object get(Object key) {
            String funcCall = (String) key;
            return "if (top == self) " + funcCall + "(); else parent.contentFrame." + funcCall + "();";
        }

        @Override
        public Object put(Object key, Object value) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Object remove(Object key) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void putAll(Map m) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void clear() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Set keySet() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Collection values() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Set entrySet() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        
    }
    
    AppletCallWrapper run;

    public String getContextPath() {
        return FacesUtils.getContextPath();
    }
    
    public String getDialogPath() {
        return FacesUtils.getDialogPath();
    }
    
    public String getRepositoryPath() {
        return FacesUtils.getRepositoryPath();
    }
    
    public String getApplicationPath() {
        return getRepositoryPath() + Constants.PATH_SEPARATOR + Constants.DIR_NODES;
    }

    public String getImagePath() {
        return getRepositoryPath() + Constants.PATH_SEPARATOR + Constants.DIR_IMAGES;
    }

    public String getCssPath() {
        return getRepositoryPath() + Constants.PATH_SEPARATOR + Constants.DIR_CSS;
    }

    public boolean isAppletSubstitution() {
        return appletSubstitution;
    }
    
    public String getClientConfigFiles() {
        if (clientConfigFiles == null) {
            StringBuilder buf = new StringBuilder();
            for (String configFile : NodesConfig.getInstance().getClientConfig().getClientConfigFiles()) {
                if (buf.length() > 0)
                    buf.append(",");
                buf.append(configFile);
            }
            
            clientConfigFiles = buf.toString();
        }
        
        return clientConfigFiles;
    }

    public AppletCallWrapper getRun() {
        return run;
    }
    
    public String getHomePageURL() {
        return DEFAULT_HOMEPAGE_URL;
    }

}
